package com.wang.template

import com.alibaba.fastjson.JSON
import com.wang.utils.StringUtils
import com.wang.template.entity.ApiTemplateInfo
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.VelocityEngine
import org.apache.velocity.runtime.RuntimeConstants
import java.io.File
import java.io.FileReader
import java.io.FileWriter

class APITemplate {
    companion object {
        fun run(templateDir: String, targetDir: String, pkg: String, inputFilePath: String) {

            File(targetDir).mkdirs()

            val ve = VelocityEngine()
            ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir)
            ve.init()

            val inputFile = File(inputFilePath)
            val s = FileReader(inputFile).readText()
            val infoList = JSON.parseArray<ApiTemplateInfo>(s, ApiTemplateInfo::class.java)


            for (apiTemplateInfo in infoList) {
                assert(apiTemplateInfo.module.isNotEmpty())

                for (file in File(templateDir).listFiles()) {
                    if (file.name.startsWith("api")) continue

                    val t = ve.getTemplate(file.name)
                    val dirAndName = file.name.replace(".vm", "").split("--")
                    // 生成文件名
                    val upperName = StringUtils.firstUpperCase(apiTemplateInfo.module)
                    val filename = dirAndName[1].replace("name", upperName)
                    val targetFileDir = targetDir + dirAndName[0].replace('.', '/')
                    File(targetFileDir).mkdirs()

                    val targetFile = File("$targetFileDir/$filename")
                    targetFile.createNewFile()
                    println(targetFile.absolutePath)

                    val ctx = VelocityContext()
                    ctx.put("pkg", pkg)
                    ctx.put("methods", apiTemplateInfo.methods)
                    ctx.put("module", apiTemplateInfo.module)
                    ctx.put("f", StringUtils)

                    val writer = FileWriter(targetFile)
                    t.merge(ctx, writer)
                    writer.flush()
                    writer.close()
                }
                println("-------------------------------------------------------------------------------")
                // 生成参数结构体
                val apiFile = File(templateDir).listFiles().filter { it.name.startsWith("api") }
                for (method in apiTemplateInfo.methods) {
                    for (apiArgTemplate in apiFile) {
                        val t = ve.getTemplate(apiArgTemplate.name)
                        val dirAndName = apiArgTemplate.name.replace(".vm", "").split("--")
                        // 生成文件名
                        val upperName = StringUtils.firstUpperCase(method.name)
                        val filename = dirAndName[1].replace("name", upperName)
                        val targetFileDir = targetDir + dirAndName[0].replace('.', '/')
                        File(targetFileDir).mkdirs()

                        val targetFile = File("$targetFileDir/$filename")
                        targetFile.createNewFile()
                        println(targetFile.absolutePath)

                        val ctx = VelocityContext()
                        ctx.put("pkg", pkg)
                        ctx.put("method", method)
                        ctx.put("module", apiTemplateInfo.module)
                        ctx.put("f", StringUtils)

                        val writer = FileWriter(targetFile)
                        t.merge(ctx, writer)
                        writer.flush()
                        writer.close()
                    }
                }
            }
        }
    }
}